<?php
    // 设置页面编码格式
    header('content-type:text/html;charset=utf-8');
    // 地址，用户名，密码。数据库名，端口号
    $sqlObj = new mysqli('127.0.0.1','root','root','ch191208cws','3306');

    if ($sqlObj -> connect_errno != 0) {
        echo '连接失败';
    }else {
       switch ($_POST['type']) {
            case 'userLogin':
                // echo '连接成功';
                $phone = $_POST['phone'];
                $pwd = $_POST['pwd'];

                // 声明sql语句     '.$phone.'     '.$_pwd.'
                $sql = 'select * from bookweb_user where bookweb_user_phone = "'.$phone.'" and bookweb_user_pwd = "'.$pwd.'" AND bookWeb_user_status = "正常"';
                // 执行sql语句 
                $result = $sqlObj -> query($sql);
                // 解析单条数据数据
                    $info = $result -> fetch_assoc();
                //  print_r ($info);
                if ($info) {
                    // echo '登入成功';
                    echo json_encode(array('code'=>10000,'msg'=>'登入成功','data'=>$phone));
                }else{
                    // echo '登入失败，账号或密码错误';
                    echo json_encode(array('code'=>10001,'msg'=>'登入失败，账号或密码错误'));
                };
                break;
           
            case 'userRegister':
                // echo '连接成功';
                $phone = $_POST['phone'];
                $pwd = $_POST['pwd'];
                
                 // 声明sql语句     '.$phone.'     '.$_pwd.'
                 $sql1 = 'select * from bookweb_user where bookweb_user_phone = "'.$phone.'"';
                 $sql2 = 'INSERT INTO bookweb_user(bookweb_user_phone,bookweb_user_pwd) VALUES("'.$phone.'","'.$pwd.'")';
                 // 执行sql语句
                  $result = $sqlObj -> query($sql1);
                 // 解析单条数据数据
                  $info = $result -> fetch_assoc();
              
                
                // 判断 info bool值
                if ($info) {
                        echo json_encode(array('code'=>10004,'msg'=>'账号已存在'));
                        // echo '账号已存在';
                }else{
                    if($sqlObj -> query($sql2)){
                        // echo '注册成功';
                        echo json_encode(array('code'=>10002,'msg'=>'注册成功'));
                    }else{
                        // echo '注册失败';
                        echo json_encode(array('code'=>10003,'msg'=>'注册失败'));
                    };
                };
                break;


            case 'adminLogin':
                // echo '连接成功';
                $count = $_POST['count'];
                $pwd = $_POST['pwd'];
                // 声明sql语句     '.$count.'     '.$pwd.'
                $sql = 'select * from bookweb_admin where bookWeb_admin_acc = "'.$count.'" and bookweb_admin_pwd = "'.$pwd.'"';
                // 执行sql语句
                $result = $sqlObj -> query($sql);
                // 解析单条数据数据
                $info = $result -> fetch_assoc();
                // print_r ($info);
                if ($info) {
                    // echo '登入成功';
                    echo json_encode(array('code'=>20000,'msg'=>'登入成功','data'=>$count));
                }else{
                    // echo '登入失败，账号或密码错误';
                    echo json_encode(array('code'=>20001,'msg'=>'登入失败，账号或密码错误'));
                };
                break;


            case 'uploadBook':
                // 获取了文件的类型
                $type = $_FILES['img']['type'];
                // 获取了文件的大小
                $size = $_FILES['img']['size'];
                // 获取了文件的临时存储路径
                $tmpName = $_FILES['img']['tmp_name'];
                if ( $type != 'image/jpeg') {
                    echo json_encode(array('code'=>10031,'msg'=>'文件类型错误'));
                }else if($size>=8388608){
                    echo json_encode(array('code'=>10032,'msg'=>'文件大小过大'));
                }else{
                    $imgPath = 'images/bookImg/';

                    // 手动创建路径 
                    if (!file_exists($imgPath)) {
                        mkdir($imgPath,'0754');
                    }
                    // 获取当前的时间戳
                    $picUrl = $imgPath.time().'.jpg';
                    $result = move_uploaded_file($tmpName, $picUrl);
                    if ($result) {
                            $sql = "INSERT INTO bookweb_book(bookWeb_book_title,bookWeb_book_detail,bookWeb_book_newPrice,bookWeb_book_oldPrice,bookWeb_book_info,bookWeb_book_Image,bookWeb_book_fenlei) VALUES('".$_POST['title']."','".$_POST['detail']."','".$_POST['newP']."','".$_POST['oldP']."','".$_POST['info']."','".$picUrl."','".$_POST['fenlei']."');";
                            
                            $result = $sqlObj->query($sql);
                            
                            if ($result) {
                                echo json_encode(array('code'=>10030,'msg'=>'上传成功'));
                            }else{
                                echo json_encode(array('code'=>10034,'msg'=>'上传失败'));
                            };
                        }else{
                        echo json_encode(array('code'=>10033,'msg'=>'上传失败'));
                    };
                };
                break;

                
            case 'bookManage':
                      
                $sql = 'SELECT * FROM bookweb_book';

                // 执行命令
                $result = $sqlObj->query($sql);
                // 解析命令
                $info = $result -> fetch_all(MYSQLI_ASSOC);

                if ($info) {
                    echo json_encode(array('code'=>10040,'msg'=>'获取成功','data'=>$info));
                }else {
                    echo json_encode(array('code'=>10041,'msg'=>'获取失败'));
                }
                break;

            // 删除图书
            case 'delBook':
                $id = $_POST['id'];

                $sql = 'DELETE FROM bookweb_book WHERE bookweb_book_id = "'.$id.'";';

                $result = $sqlObj->query($sql);

                
                if ($result) {
                    $sql = 'SELECT * FROM bookweb_book';

                    // 执行命令
                    $result = $sqlObj->query($sql);
                    // 解析命令
                    $info = $result -> fetch_all(MYSQLI_ASSOC);
                    echo json_encode(array('code'=>10050,'msg'=>'删除成功','data'=>$info));
                }else {
                    echo json_encode(array('code'=>10051,'msg'=>'删除失败'));
                }
                break;

                // 用户管理
                case 'userManage':
                      
                    $sql = 'SELECT * FROM bookweb_user';
    
                    // 执行命令
                    $result = $sqlObj->query($sql);
                    // 解析命令
                    $info = $result -> fetch_all(MYSQLI_ASSOC);
    
                    if ($info) {
                        echo json_encode(array('code'=>10060,'msg'=>'获取成功','data'=>$info));
                    }else {
                        echo json_encode(array('code'=>10061,'msg'=>'获取失败'));
                    }
                    break;
                    
                // 用户封禁
                case 'closeUser':
                    $id = $_POST['id'];
                    
                    
                    $status = $_POST['status'];
                    
                    // echo $status;
                    if ($status == '封禁') {
                        $sql = 'update bookweb_user set bookWeb_user_status= "正常" where bookWeb_user_id ="'.$id.'";';

                        $result = $sqlObj->query($sql);
                    
                        if ($result) {
                            $sql = 'SELECT * FROM bookweb_user';   
                            // 执行命令
                            $result = $sqlObj->query($sql);
                            // 解析命令
                            $info = $result -> fetch_all(MYSQLI_ASSOC);
                            echo json_encode(array('code'=>10070,'msg'=>'解禁成功','data'=>$info));
                        }else {
                            echo json_encode(array('code'=>10071,'msg'=>'封禁失败'));
                        }
                    }else {
                        $sql = 'update bookweb_user set bookWeb_user_status= "封禁" where bookWeb_user_id ="'.$id.'";';

                        $result = $sqlObj->query($sql);
                        if ($result) {
                            $sql = 'SELECT * FROM bookweb_user';   
                            // 执行命令
                            $result = $sqlObj->query($sql);
                            // 解析命令
                            $info = $result -> fetch_all(MYSQLI_ASSOC);
                            echo json_encode(array('code'=>10070,'msg'=>'封禁成功','data'=>$info));
                        }else {
                            echo json_encode(array('code'=>10071,'msg'=>'封禁失败'));
                        }
                    }

    
            
                    break;

                case 'getBook':

                    $type = $_POST['bookType'];
                    
                    $sql = 'SELECT * FROM bookweb_book where bookWeb_book_fenlei = "'.$type.'" LIMIT 10;';
    
                    // 执行命令
                    $result = $sqlObj->query($sql);
                    // 解析命令
                    $info = $result -> fetch_all(MYSQLI_ASSOC);
    
                    if ($info) {
                        echo json_encode(array('code'=>20010,'msg'=>'获取成功','data'=>$info));
                    }else {
                        echo json_encode(array('code'=>20011,'msg'=>'获取失败'));
                    }
                    break;
                
                // 书本详情页
                case 'bookPage':

                    $bookId = $_POST['bookId'];
                    
                    $sql = 'SELECT * FROM bookweb_book where bookWeb_book_id = "'.$bookId.'";';
    
                    // 执行命令
                    $result = $sqlObj->query($sql);
                    // 解析命令
                    $info = $result -> fetch_all(MYSQLI_ASSOC);
    
                    if ($info) {
                        echo json_encode(array('code'=>20020,'msg'=>'获取成功','data'=>$info));
                    }else {
                        echo json_encode(array('code'=>20021,'msg'=>'获取失败'));
                    }
                    break;

                // 下单
                case 'createOrder':
                    $userID = $_POST['userID'];
                    $bookID = $_POST['bookID'];
                    $bookTitle = $_POST['bookTitle'];
                    $bookPrice = $_POST['bookPrice'];

                    
                    $sql = 'INSERT INTO bookweb_order(bookWeb_order_phone,bookWeb_order_goods,bookWeb_order_goods_title,bookWeb_order_goods_price) VALUES("'.$userID.'","'.$bookID.'","'.$bookTitle.'","'.$bookPrice.'");';
    
                    // // 执行命令
                    // $result = $sqlObj->query($sql);
                    // // 解析命令
                    // $info = $result -> fetch_all(MYSQLI_ASSOC);
    
                    if ($sqlObj->query($sql)) {
                        echo json_encode(array('code'=>30001,'msg'=>'已成功加入购物车'));
                    }else {
                        echo json_encode(array('code'=>30001,'msg'=>'订单失败'));
                    }
                    break;



                // 获取用户个人信息
                case 'getUserinfo':
                    $phone = $_POST['phone'];
                    
                    $sql = 'SELECT * FROM bookweb_user where bookWeb_user_phone = "'.$phone.'";';
    
                    // 执行命令
                    $result = $sqlObj->query($sql);
                    // 解析命令
                    $info = $result -> fetch_assoc();
    
                    if ($sqlObj->query($sql)) {
                        echo json_encode(array('code'=>30010,'msg'=>'获取个人信息成功','data'=>$info));
                    }else {
                        echo json_encode(array('code'=>30011,'msg'=>'获取个人信息失败'));
                    }
                    break;

                // 加钱充值
                case 'addMoney':
                    $phone = $_POST['phone'];
                    $money = $_POST['money'];
                    
                    $sql = 'update bookweb_user set bookWeb_user_money=bookweb_user.bookWeb_user_money +"'.$money.'"  where bookWeb_user_phone ="'.$phone.'";';
    
                    // 执行命令
                    // $result = $sqlObj->query($sql);
                    // 解析命令
                    // $info = $result -> fetch_assoc();
    
                    if ($sqlObj->query($sql)) {
                        $sql = 'SELECT * FROM bookweb_user where bookWeb_user_phone ="'.$phone.'";';   
                        // 执行命令
                        $result = $sqlObj->query($sql);
                        // 解析命令
                        $info = $result -> fetch_assoc();
                        echo json_encode(array('code'=>30020,'msg'=>'充值成功','data'=>$info));
                    }else {
                        echo json_encode(array('code'=>30021,'msg'=>'获取个人信息失败'));
                    }
                    break;

                    
                // 未支付
                case 'Trade':
                    // 书籍ID
                    $phone = $_POST['phone'];
                    
                    // 查找书籍信息
                    $sql = 'SELECT * FROM bookweb_order where bookWeb_order_phone ="'.$phone.'";';
                    // 执行命令
                    $result = $sqlObj->query($sql);
                    // 解析命令
                    $info = $result -> fetch_all(MYSQLI_ASSOC);
                    if ($info) {
                        echo json_encode(array('code'=>40000,'msg'=>'获取订单书籍信息成功','orderData'=>$info));
                    }else {
                        echo json_encode(array('code'=>40001,'msg'=>'获取订单书籍信息失败'));
                    }
                    break;  


                // 付款
                case 'payment':
                    // 用户账号
                    $phone = $_POST['phone'];
                    // 书籍价格
                    $bookPrice = $_POST['bookPrice'];
                    // 书籍ID
                    $bookID = $_POST['bookID'];
                    
                    // 从账号中扣钱
                    $sql1 = 'update bookweb_user set bookWeb_user_money=bookweb_user.bookWeb_user_money -"'.$bookPrice.'"  where bookWeb_user_phone ="'.$phone.'";';
                    // 修改对应账号的对应的书籍ID的订单状态
                    $sql2 = 'update bookweb_order set bookWeb_order_status = "已付款"  where bookWeb_order_phone ="'.$phone.'" AND bookWeb_order_goods = "'.$bookID.'" ;';

                    if ($sqlObj->query($sql1) && $sqlObj->query($sql2)) {
                        // 查询账号所有信息 目的是为了拿到账号里的钱
                        $sql1 = 'SELECT * FROM bookweb_user where bookWeb_user_phone ="'.$phone.'";';   
                        // 再次查询该账号的所有订单
                        $sql2 = 'SELECT * FROM bookweb_order where bookWeb_order_phone ="'.$phone.'";';
                        // 执行命令
                        $result1 = $sqlObj->query($sql1);
                        // 解析命令
                        $info1 = $result1 -> fetch_assoc();

                        // 执行命令
                        $result2 = $sqlObj->query($sql2);
                        // 解析命令
                        $info2 = $result2 -> fetch_all(MYSQLI_ASSOC);
                        echo json_encode(array('code'=>30030,'msg'=>'付款成功','userData'=>$info1,'orderData'=>$info2));
                    }else {
                        echo json_encode(array('code'=>30031,'msg'=>'付款失败'));
                    }
                    break;

                case 'checkReceive':
                    // 用户账号
                    $phone = $_POST['phone'];
                    // 书籍ID
                    $bookID = $_POST['bookID'];
                    
                    // 修改对应账号的对应的书籍ID的订单状态
                    $sql = 'update bookweb_order set bookWeb_order_status = "已收货"  where bookWeb_order_phone ="'.$phone.'" AND bookWeb_order_goods = "'.$bookID.'" ;';

                    if ($sqlObj->query($sql)) { 
                        // 再次查询该账号的所有订单
                        $sql = 'SELECT * FROM bookweb_order where bookWeb_order_phone ="'.$phone.'";';
                        // 执行命令
                        $result = $sqlObj->query($sql);
                        // 解析命令
                        $info = $result -> fetch_all(MYSQLI_ASSOC);
                        echo json_encode(array('code'=>30040,'msg'=>'收货成功','orderData'=>$info));
                    }else {
                        echo json_encode(array('code'=>30041,'msg'=>'收货失败'));
                    }
                    break;
                    
                    
                case 'evaluate':
                    // 用户账号
                    $phone = $_POST['phone'];
                    // 书籍ID
                    $bookID = $_POST['bookID'];
                    // 用户评价
                    $evaluate = $_POST['evaluate'];
                    
                    // 修改对应账号的对应的书籍ID的订单状态
                    $sql1 = 'update bookweb_order set bookWeb_order_goods_evaluate = "'.$evaluate.'"  where bookWeb_order_phone ="'.$phone.'" AND bookWeb_order_goods = "'.$bookID.'" ;';
                    $sql2 = 'update bookweb_order set bookWeb_order_status = "已评价"  where bookWeb_order_phone ="'.$phone.'" AND bookWeb_order_goods = "'.$bookID.'" ;';

                    if ($sqlObj->query($sql1) && $sqlObj->query($sql2)) { 
                        // 再次查询该账号的所有订单
                        $sql = 'SELECT * FROM bookweb_order where bookWeb_order_phone ="'.$phone.'";';
                        // 执行命令
                        $result = $sqlObj->query($sql);
                        // 解析命令
                        $info = $result -> fetch_all(MYSQLI_ASSOC);
                        echo json_encode(array('code'=>30050,'msg'=>'评价成功','orderData'=>$info));
                    }else {
                        echo json_encode(array('code'=>30051,'msg'=>'收货失败'));
                    }
                    break;

                default:
                    # code...
                    break;
       }
    }
?>